<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    width="50%"
    @close="btnCancel"
  >
    <el-form ref="employeeData" :model="employData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employData.username" style="width: 50%" placeholder="1-4字符" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employData.mobile" style="width: 40%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employData.timeOfEntry" placeholder="选择日期" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employData.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employData.workNumber" style="width: 50%" placeholder="1-20个字符" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="employData.departmentName" style="width: 40%" placeholder="请选择" @focus="getDepartments" />
        <el-tree v-if="showTree" v-loading="loading" :default-expand-all="true" :data="departmentData" :props="defaultProps" @node-click="selectDepart" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="employData.correctionTime" placeholder="选择日期" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" class="dialog-footer">
      <el-button size="small" @click="btnCancel">取 消</el-button>
      <el-button size="small" type="primary" @click="btnOk">确 定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      employData: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            message: '员工姓名不能为空'
          },
          {
            min: 1,
            max: 4,
            trigger: 'blur',
            message: '员工姓名不能超过30个字符'
          }
        ],
        mobile: [
          {
            required: true,
            trigger: 'blur',
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            trigger: 'blur',
            message: '手机格式不正确'
          }
        ],
        formOfEmployment: [
          {
            required: true,
            trigger: 'blur',
            message: '聘用形式不能为空'
          }
        ],
        workNumber: [
          {
            required: true,
            trigger: 'blur',
            message: '工号不能为空'
          }
        ],
        departmentName: [
          {
            required: true,
            trigger: 'change',
            message: '部门名称不能为空'
          }
        ],
        timeOfEntry: [
          {
            required: true,
            trigger: 'blur',
            message: '入职时间不能为空'
          }
        ],
        correctionTime: [
          {
            required: true,
            trigger: 'blur',
            message: '手机号不能为空'
          }
        ]
      },
      defaultProps: {
        label: 'name'
      },
      departmentData: [], // 存储组织架构数据
      loading: false,
      showTree: false, // 控制el-tree的显示
      EmployeeEnum // 静态员工参数
    }
  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      // 将获取的组织架构数据转换为树形结构数组并赋值给el-tree的渲染data数据
      this.departmentData = tranListToTreeDate(depts, '')
      this.loading = false
    },
    selectDepart(node) {
      this.employData.departmentName = node.name
      this.showTree = false
    },
    async btnOk() {
      try {
        // 手动校验
        await this.$refs.employeeData.validate
        // 发起新增员工请求
        await addEmployee(this.employData)
        // 重新拉取数据
        // this.$parent 相当于父组件中的this
        this.$parent.getEmployeeList && this.$parent.getEmployeeList()
        // 关闭新增员工弹窗
        this.$parent.showDialog = false
        // console.log(this.$parent);
      // this.$emit('update:showDialog', false)
      } catch (error) {
        console.log(error)
      }
    },
    async btnCancel() {
      console.log('调用了btncancel')
      // 重置表单
      this.employData = {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      this.$refs.employeeData.resetFields() // 重置校验提示
      this.$emit('update:showDialog', false)
      // this.$parent.showDialog = false
    }
  }
}
</script>

<style>

</style>
